/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cnam.mobileshopping.ejb.product;

import java.util.Date;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.cnam.mobileShopping.entity.Category;
import org.cnam.mobileShopping.entity.Product;

/**
 *
 * @author Administrateur
 */
@Stateless(mappedName="ejb/mobileshopping/product")
@Local(ProductLocal.class)
@Remote(ProductRemote.class)
public class ProductBean implements ProductLocal, ProductRemote{

    @PersistenceContext(unitName="mobileshopping-corePU")
    EntityManager entityManager;
    
    @Override
    public Product getProduct(Integer id) {
        if(id != null){
          java.util.List<Product> products = entityManager.createNamedQuery("Product.findByProductid").setParameter("productid", id).getResultList();
          if(products != null && products.size() == 1){
              return products.get(0);
          }else{
              return null;
          }
        } else {
            throw new NullPointerException("id is null");
        }
    }

    @Override
    public List<Product> getProductsByCategory(Integer categoryId) {
         if(categoryId != null){
             Category category = entityManager.find(Category.class, categoryId);
          java.util.List<Product> products = entityManager.createNamedQuery("Product.findByCategory").setParameter("categoryid", category).getResultList();
          return products;
        } else {
            throw new NullPointerException("categoryId is null");
        }
    }

    @Override
    public List<Category> getCategories() {
          java.util.List<Category> categories = entityManager.createNamedQuery("Category.findAll").getResultList();
          return categories;
    }
    
    public Category getCategory(Integer id) {
          java.util.List<Category> categories = entityManager.createNamedQuery("Category.findByCategoryid").setParameter("categoryid", id).getResultList();
          if(categories != null && categories.size() == 1) {
              return categories.get(0);
          }
          return null;
    }

    @Override
    public Product addProduct(String description, Integer categoryId, String barcode) {
        if(description != null && categoryId != null && barcode != null) {
            Category cat = getCategory(categoryId);
            if(cat != null) {
                Product p = new Product();
                p.setBarcode(barcode);
                p.setCategoryid(cat);
                p.setDescription(description);
                p.setLastupdatedatetime(new Date());
                entityManager.persist(p);
                return p;
            }
        } else {
            throw new NullPointerException("description is null or categoryId is null or barcode is null");
        }
        return null;
    }

    @Override
    public List<Product> getProductsForSynchro(Date lastupdate) {
       java.util.List<Product> products = entityManager.createNamedQuery("Product.findAfterDate").setParameter("lastupdate", lastupdate).getResultList();
       return products;
    }
    
    
    
}
